import axios from 'axios'
  // @ts-ignore
import { useStore } from '../store'

const store = useStore();

const apiBaseUrl = 'http://localhost:9000'

const apiSet = {
    //取得当日时间，格式YYYY/MM/DD
    getDate : () => {return new Date().toISOString().split('T')[0].replace(/-/g, '/')},
    getUserInfo: async (cardNumber: any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/getUserInfo`,
                method: 'post',
                data: {
                    userCardNumber: cardNumber,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            });
            console.log('=====获取用户信息成功=====')
            if (res.data.data.length == 0) {
                return {
                    status: 404,
                    message: '卡号不存在'
                }
            } else if (res.data.data[0].cardNumber == cardNumber) {
                store.setUserInfo(res.data.data[0])
                return {
                    status: 200,
                    message: '登录成功'
                }
            } else {
                return {
                    status: 500,
                    message: '登录失败'
                }
            }
        } catch (error) {
            console.error(error);
            throw error;
        }
    },

    //刷新用户信息
    refreshUserInfo: async (cardNumber: any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/getUserInfo`,
                method: 'post',
                data: {
                    userCardNumber: cardNumber,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            });
            console.log('=====获取用户信息成功=====')
            if (res.data.data[0].cardNumber == cardNumber) {
                store.setUserInfo(res.data.data[0])
                return {
                    status: 200,
                    message: '登录成功'
                }
            } else {
                return {
                    status: 500,
                    message: '刷新失败'
                }
            }
        } catch (error) {
            console.error(error);
            throw error;
        }
    },  

    //管理员登录
    adminlogin: async (adminAcc: any, adminPwd : any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/adminLogin`,
                method: 'post',
                data: {
                    adminAcc,
                    adminPwd,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            })
            console.log('=====管理员信息获取成功=====')
            return res.data
                // if (res.data.status == 500 || res.data.status == 401) {
                //     ElMessage.error(res.data.message)
                // } else {
                //     ElMessage.success('登陆成功')
                //     store.setAdminName(data.adminAcc)
                //     route.push('/dashboard')
                // }
        } catch (error) {
            console.error(error);
            throw error;
        }
    },

    //结算购买商品
    settlement: async (userinfo: any, cart: any, imageSrc: any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/purchaseItems`,
                method: 'post',
                data: {
                    userinfo,
                    cart,
                    date: apiSet.getDate(),
                    imageSrc,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            });
            console.log('=====结算成功=====')
            return res.data
        } catch (error) {
            console.error(error);
            throw error;
        }
    },

    //按日期查询订单
    getOrdersByDate: async (date: any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/getOrdersByDate`,
                method: 'post',
                data: {
                    date,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            });
            console.log('=====订单查询成功=====')
            return res.data
        } catch (error) {
            console.error(error);
            throw error;
        }
    },

    //充值
    recharge: async (cardNumber: any, rechargeAmount: any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/recharge`,
                method: 'post',
                data: {
                    cardNumber,
                    rechargeAmount,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            });
            console.log('=====充值成功=====')
            return res.data
        } catch (error) {
            console.error(error);
            throw error;
        }
    },

    //创建用户
    createUser: async (userInfo: any) => {
        try {
            const res = await axios({
                url: `${apiBaseUrl}/createUser`,
                method: 'post',
                data: {
                    userInfo,
                },
                headers: {
                    'Content-Type': "application/json"
                }
            });
            return res.data
        } catch (error) {
            console.error(error);
            throw error;
        }
    },
}

export default apiSet
